﻿using System;
using System.Security.Cryptography;
using System.Text;

namespace Yoozoo.Utils
{ 
    public class GStringUtils
    {
        public const string UTF_16_Sign = "\\u";
        public const string UTF_32_Sign = "u+";

        public static string Parse2UTF_16(string str, bool showSign = true)
        {
            var bytes = Encoding.Unicode.GetBytes(str);
            StringBuilder sb;
            if (showSign)
            {
                sb = new StringBuilder(bytes.Length * 3);
                for (var i = 0; i < bytes.Length; i += 2)
                {
                    sb.Append(UTF_16_Sign);
                    sb.AppendFormat("{0:x2}{1:x2}", bytes[i + 1], bytes[i]);
                }
            }
            else
            {
                sb = new StringBuilder(bytes.Length * 2);
                for (var i = 0; i < bytes.Length; i += 2)
                    sb.AppendFormat("{0:x2}{1:x2}", bytes[i + 1], bytes[i]);
            }

            return sb.ToString();
        }

        public static string Parse2UTF_32(string str, bool showSign = true)
        {
            var bytes = Encoding.UTF32.GetBytes(str);
            StringBuilder sb;
            if (showSign)
            {
                sb = new StringBuilder(bytes.Length / 4 * 10);
                for (var i = 0; i < bytes.Length; i += 4)
                {
                    sb.Append(UTF_32_Sign);
                    sb.AppendFormat("{0:x2}{1:x2}{2:x2}{3:x2}", bytes[i + 3], bytes[i + 2], bytes[i + 1], bytes[i]);
                }
            }
            else
            {
                sb = new StringBuilder(bytes.Length * 2);
                for (var i = 0; i < bytes.Length; i += 4)
                    sb.AppendFormat("{0:x2}{1:x2}{2:x2}{3:x2}", bytes[i + 3], bytes[i + 2], bytes[i + 1], bytes[i]);
            }

            return sb.ToString();
        }

        // 获取字符串内真正代表数字的部分
        public static string GetValidHexString(string str)
        {
            if (string.IsNullOrEmpty(str))
                return str;
            
            var arr = str.ToCharArray();
            var firstValidIndex = -1;
            for (var i = 0; i < arr.Length; i++)
            {
                if (arr[i] < '0' || arr[i] > 'f')
                {
                    if (firstValidIndex != -1)
                        return string.Empty;
                    
                    continue;   
                }

                if (arr[i] == '0' && firstValidIndex == -1)
                    continue;

                if (firstValidIndex == -1)
                    firstValidIndex = i;
            }

            if (firstValidIndex == -1)
                return string.Empty;

            return str.Substring(firstValidIndex);
        }

        public static string Parse2MD5(string str)
        {
            var bytes = Encoding.UTF8.GetBytes(str);
            var md5 = new MD5CryptoServiceProvider();
            var output = md5.ComputeHash(bytes);
            var sb = new StringBuilder();
            for (int i = 0; i < output.Length; i++)
                sb.Append(Convert.ToString(output[i], 16).PadLeft(2, '0'));

            return sb.ToString();
        }
    }   
}